/*
 * @lc app=leetcode.cn id=621 lang=javascript
 *
 * [621] 任务调度器
 */

// @lc code=start
/**
 * @param {character[]} tasks
 * @param {number} n
 * @return {number}
 */
var leastInterval = function(tasks, n) {
    let map=new Map()
    for(let item of tasks){
        map.set(item,map.get(item)?map.get(item)+1:1)
    }
    let arr=[...(map.values())].sort((a,b)=>b-a)
    let num=arr[0]
    let res=(num-1)*(n+1)+1
    let i=1
    while(arr[i]==num&&i<arr.length){
        res++
        i++
    }
    return Math.max(tasks.length,res)
};
// @lc code=end

leastInterval (["A","A","A","B","B","B"],2)